package br.com.webhomesites.padrao.presenter;

import br.com.webhomesites.padrao.contracts.IClientePresenter;
import br.com.webhomesites.padrao.contracts.IClienteView;
import br.com.webhomesites.padrao.utils.Mascarador;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JOptionPane;

public class ClientePresenterActionListeners {  
      
    static class AlterarActionListener implements ActionListener {  
        private ClientePresenter presenter;  
          
        public AlterarActionListener(ClientePresenter presenter) {  
            this.presenter = presenter;  
        }  
          
        @Override
        public void actionPerformed(ActionEvent evt) {  
            presenter.setOperacao(presenter.UPDATE_STRATEGY);  
            presenter.habilitarEdicao(true); 
            
              
            IClienteView view = (IClienteView)presenter.getView();  
              
            view.enableBtnInserir(true);  
            view.enableBtnRemover(true);
            view.enableBtnCancelar(true);  
            view.enableBtnSalvar(true); 
            view.enableBtnAlterar(true); 
     
        }  
    }  
      
    static class CancelarActionListener implements ActionListener {  
        private ClientePresenter presenter;  
          
        public CancelarActionListener(ClientePresenter presenter) {  
            this.presenter = presenter;  
        }  
          
        @Override
        public void actionPerformed(ActionEvent evt) {  
            
            IClienteView view = (IClienteView)presenter.getView();  
              
            view.clearFields();  
              
            //presenter.habilitarEdicao(false);  
            
            view.enableBtnInserir(true);  
            view.enableBtnRemover(false);
            view.enableBtnCancelar(false);  
            view.enableBtnSalvar(false); 
            view.enableBtnAlterar(false);              

        }  
    }  
      
    static class SalvarActionListener implements ActionListener {  
        private ClientePresenter presenter;  
          
        public SalvarActionListener(ClientePresenter presenter) {  
            this.presenter = presenter;  
        }  
          
        @Override
        public void actionPerformed(ActionEvent evt) {  
            presenter.getOperacao().execute();
            //presenter.setOperacao(presenter.UPDATE_STRATEGY);
            //presenter.habilitarEdicao(false);  
            
            //setUpViewListeners();
              
            IClienteView view = (IClienteView)presenter.getView();  
              
            view.enableBtnInserir(true);
            view.enableBtnRemover(false);
            view.enableBtnCancelar(false);  
            view.enableBtnSalvar(false); 
            view.enableBtnAlterar(false);  
                        
            System.out.println("Passou na ação salvar!");
  
        }  
    }  
      
    static class InserirActionListener implements ActionListener {  
        private ClientePresenter presenter;  
        
        private static int codigocliente = 0;
        
        public InserirActionListener(ClientePresenter presenter) {  
            this.presenter = presenter;  
        }  
          
        @Override
        public void actionPerformed(ActionEvent evt) {  
            presenter.setOperacao(presenter.INSERT_STRATEGY);              
            presenter.novoCliente();  
                          
            IClienteView view = (IClienteView)presenter.getView();  
                                    
            presenter.habilitarEdicao(true);  
            view.clearFields();  
            codigocliente = codigocliente+1;
            view.setCodigo(codigocliente);
                          
            view.enableBtnInserir(false);  
            view.enableBtnRemover(false);
            view.enableBtnCancelar(true);  
            view.enableBtnSalvar(true); 
            view.enableBtnAlterar(false);
        }  
    }  
      
    static class RemoverActionListener implements ActionListener {  
        private ClientePresenter presenter;  
          
        public RemoverActionListener(ClientePresenter presenter) {  
            this.presenter = presenter;  
        }  
          
        @Override
        public void actionPerformed(ActionEvent evt) {  
              
            if (JOptionPane.showConfirmDialog((Component)presenter.getView(), "Deseja remover?", "Pergunta", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {  
                  
                IClienteView view = (IClienteView)presenter.getView();  
                  
                presenter.removerCliente();  
                view.clearFields();  
                
                view.enableBtnInserir(true);
                view.enableBtnAlterar(false);  
                view.enableBtnRemover(false);  
            }  
        }  
    }  
      
    static class SairActionListener implements ActionListener {  
        @Override
        public void actionPerformed(ActionEvent evt) {  
            System.exit(0);  
        }  
    }
    
    static class CampoKeyListener implements KeyListener {
        private IClientePresenter presenter;
        IClienteView view;
        Mascarador mask;  
        
        public CampoKeyListener(IClientePresenter presenter) {  
            this.presenter = presenter;  
            this.view = (IClienteView)presenter.getView();
            mask = new Mascarador();
        } 
               
        @Override
        public void keyTyped(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void keyPressed(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void keyReleased(KeyEvent e) {            
            view.setCpf(mask.getMascara(view.getCpf(), mask.CPF, e));
            view.setCnpj(mask.getMascara(view.getCnpj(), mask.CNPJ, e));
            view.setRg(mask.getMascara(view.getRg(), mask.RG, e));
            view.setTelefone(mask.getMascara(view.getTelefone(), mask.TELEFONE, e));
            view.setCelular(mask.getMascara(view.getCelular(), mask.TELEFONE, e));
        }                
    }
    
}  